<template>
    <div>
        <!-- 导航部分开始 -->
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>设备管理</el-breadcrumb-item>
            <el-breadcrumb-item>点检保养项目</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 导航部分结束 -->
        <!--  搜索栏开始 -->
        <el-form style="margin-top: 15px;" :inline="true" :model="querySub" class="demo-form-inline">
            <el-form-item label="项目编码">
                <el-input v-model="querySub.subjectCode" placeholder="请输入项目编码" clearable />
            </el-form-item>
            <el-form-item label="项目名称">
                <el-input v-model="querySub.subjectName" placeholder="请输入项目名称" clearable />
            </el-form-item>

            <el-form-item label="项目类型">
                <el-select v-model="querySub.subjectType" placeholder="请选择项目类型">
                    <el-option label="设备点检" value="设备点检" />
                    <el-option label="设备保养" value="设备保养" />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="loadSub">搜索</el-button>
            </el-form-item>
            <el-form-item>
                <el-button @click="clearSubjectType"><el-icon>
                        <Refresh />
                    </el-icon>重置</el-button>
            </el-form-item>
        </el-form>
        <!--  搜索栏结束 -->
        <!-- 工具栏开始 -->
        <div class="flex">
            <el-button @click="this.addSubjectDialogVisible = true" type="primary" plain><el-icon>
                    <Plus />
                </el-icon>
                新增
            </el-button>
            <el-button type="success" plain><el-icon>
                    <EditPen />
                </el-icon>
                修改
            </el-button>
            <el-button type="danger" plain><el-icon>
                    <Delete />
                </el-icon>
                删除
            </el-button>
            <el-button type="warning" plain><el-icon>
                    <Download />
                </el-icon>
                导出
            </el-button>
        </div>
        <!-- 工具栏结束 -->
       <!-- 表格 开始 -->
       <el-table  :data="Sub" max-height="800px" stripe="true" border="true"
            style="width: 90%;margin-top: 10px;">
            <el-table-column align="center" type="selection" width="55" />
            <el-table-column align="center" label="项目编码" property="subjectCode" />
            <el-table-column align="center" label="项目类型" property="subjectType" />
            <el-table-column align="center" label="项目内容" property="subjectContent"/>
            <el-table-column align="center" label="标准" property="subjectStandard" />
            <el-table-column align="center" label="是否启用" property="enableFlag" />
            <el-table-column align="center" label="备注" property="remark" />
            <el-table-column width="150" align="center" label="操作">
                <template #default="scope">
                    <el-button size="small" @click="handleEdit(scope.row)">修改</el-button>
                    <el-popconfirm confirm-button-text="是" cancel-button-text="点错了" @confirm="handleDelete(scope.row)"
                        title="您是否要删除这行数据">
                        <template #reference>
                            <el-button size="small" type="danger">删除</el-button>
                        </template>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <!-- 表格 结束 -->
         <!-- 分页栏开始 -->
         <el-pagination style="margin-top: 15px;" :page-size="SubLimit" :pager-count="SubMaxPage"
            @current-change="changePage" background layout="prev, pager, next" :total="SubTotal" />
        <!-- 分页栏结束 -->
        <!-- 添加项目的弹窗窗体开始-->
        <el-dialog v-model="addSubjectDialogVisible" title="添加项目信息" width="600" align-center>
            <!-- 添加项目的表单 -->
            <el-form label-width="100px" :model="newSub" style="max-width: 460px">
                <el-form-item label="项目编码">
                    <el-input v-model="newSub.subjectCode" />
                </el-form-item>
                <el-form-item label="项目名称">
                    <el-input v-model="newSub.subjectName" />
                </el-form-item>
                <el-form-item label="项目类型">
                    <el-input v-model="newSub.subjectType" />
                </el-form-item>
                <el-form-item label="是否启用">
                    <el-input v-model="newSub.enableFlag" />
                </el-form-item>
                <el-form-item label="项目内容">
                    <el-input v-model="newSub.subjectContent" />
                </el-form-item>
                <el-form-item label="标准">
                    <el-input v-model="newSub.subjectStandard" />
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="cancelAddSub">取消</el-button>
                    <el-button type="primary" @click="doAddSub">
                        提交
                    </el-button>
                </span>
            </template>
        </el-dialog>
        <!-- 添加项目的弹窗窗体结束-->
          <!--修改项目的抽屉弹窗开始 -->
          <el-drawer v-model="SubEditDrawer" :direction="direction">
            <template #header>
                <h4>修改项目信息</h4>
            </template>
            <template #default>
                <div>
                    <!-- 修改项目的表单 -->
                    <el-form label-width="100px" :model="editSub" style="max-width: 460px">
                        <el-form-item label="项目编码">
                    <el-input v-model="editSub.subjectCode" />
                </el-form-item>
                <el-form-item label="项目名称">
                    <el-input v-model="editSub.subjectName" />
                </el-form-item>
                <el-form-item label="项目类型">
                            <el-radio-group v-model="editSub.subjectType" class="ml-4">
                                <el-radio label="设备点检">设备点检</el-radio>
                                <el-radio label="设备保养">设备保养</el-radio>
                            </el-radio-group>
                        </el-form-item>
                <el-form-item label="是否启用">
                            <el-radio-group v-model="editSub.enableFlag" class="ml-4">
                                <el-radio label="Y">Y</el-radio>
                                <el-radio label="N">N</el-radio>
                            </el-radio-group>
                        </el-form-item>
                <el-form-item label="项目内容">
                    <el-input v-model="editSub.subjectContent" />
                </el-form-item>
                <el-form-item label="标准">
                    <el-input v-model="editSub.subjectStandard" />
                </el-form-item>   
                    </el-form>
                </div>
            </template>
            <template #footer>
                <div style="flex: auto">
                    <el-button @click="cancelEdit">取消</el-button>
                    <el-button type="primary" @click="doSubEdit">提交</el-button>
                </div>
            </template>
        </el-drawer>
        <!-- 编辑用户的抽屉弹窗结束 -->
    </div>
</template>
<script>
import SubRequest from '../../../api/maintenance-items-api.js';
export default {
    data() {
        return {
            Sub: [],//设备类型数据列表
            querySub:{},
            SubCurrPage: 1, // 项目分页的当前页码
            SubLimit: 10, // 项目分页的每页大小
            SubTotal: 0, // 项目分页的总条数
            SubMaxPage: 1,// 项目分页的最大页码
            newSub: {},//新添加的项目对象
            editSub:{},// 当前正在修改的项目对象
            SubEditDrawer: false,//标记修改项目信息的窗体是否显示
            direction: 'rtl',// 抽屉的方向

            addSubjectDialogVisible: false,//新增设备类型弹窗标志
        }
    },
    methods: {

        /**
        * 项目修改提交按钮事件
        */
        doSubEdit() {
            // 提交editUser即可
            SubRequest.edit(this.editSub, res => {
                // 这里的res就是我们的后端返回的MESResult对象
                if (res.code == '200') {
                    this.$message({
                        message: '修改项目成功',
                        type: 'success'
                    });
                    //关闭窗体
                    this.SubEditDrawer = false;
                    // 重新加载数据
                    this.loadSub();
                    // 将editSub制空
                    this.editSub = {};
                } else if (res.code == '300') {
                    this.$message({
                        message: '项目名重复',
                        type: 'warning',
                    });
                } else {
                    this.$message({
                        message: '修改失败',
                        type: 'error',
                    });
                }
            });
        },
        /**
         * 取消用户编辑操作
         */
        cancelEdit() {
            // 将编辑的设备类型制空
            this.editSub = {};
            // 关闭抽屉弹窗
            this.SubEditDrawer = false;
        },
        /**
        * 行编辑按钮事件
        * @param {*} row 整行数据 
        */
        handleEdit(row) {
            // 通过id加载要编辑的用户信息
            SubRequest.get(row.subjectId, res => {
                // 将查询的用户信息设置到editSUb中
                this.editSub = res.data;
                // 显示抽屉弹窗
                this.SubEditDrawer = true;
            });
        },
           /**
         * 行删除按钮事件
         * @param {*} row 整行数据
         */
         handleDelete(row) {
            SubRequest.delete(row.subjectId,res => {
                if (res.code == 200) {
                    this.$message({
                        type: "success",
                        message: "删除成功"
                    });
                    this.loadSub();
                } else {
                    this.$message({
                        type: "error",
                        message: "删除失败"
                    });
                }
            })
        },
         /**
         * 添加用户的提交按钮
         */
         doAddSub() {
            SubRequest.save(this.newSub, res => {
                // 这里的res就是我们的后端返回的MESResult对象
                if (res.code == '200') {
                    this.$message({
                        message: '添加用户成功',
                        type: 'success'
                    });
                    //关闭窗体
                    this.addSubjectDialogVisible = false;
                    // 重新加载数据
                    this.loadDv();
                    // 将newSub制空
                    this.newSub = {};
                } else if (res.code == '300') {
                    this.$message({
                        message: '用户名重复',
                        type: 'warning',
                    });
                } else {
                    this.$message({
                        message: '添加失败',
                        type: 'error',
                    });
                }
            });
        },
        /**
         * 取消添加用户的操作
         */
        cancelAddSub() {
            // 将newSub制空
            this.newSub = {};
            // 关闭窗体
            this.addSubjectDialogVisible = false;
        },
        /**
         * 项目列表的翻页函数
         * @param {} value  最新页码
         */
        changePage(value) {
            this.SubCurrPage = value;
            this.loadSub();
        },
        /**
         * 加载设备类型数据
         */
        loadSub() {
            SubRequest.list(this.SubCurrPage, this.SubLimit, this.querySub, res => {
                // 设置数据
                this.Sub = res.data.data;
                this.SubTotal = res.data.total;
                this.SubMaxPage = res.data.pages;
            });
          
        },
        clearSubjectType() {
            this.querySub.subjectCode='',
            this.querySub.subjectName='',
            this.querySub.subjectType='',
            this.loadSub();
        }
    },
    mounted() {
        /**
         * 默认加载数列表
         */
        this.loadSub();
    }

}

</script>

